package com.rx.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;

public class PmFullFaction {

	private static final String PREFIX = "com.rx";

	private enum Level {
		action, biz, dao, entity;
	}

	private static String srcPath = "F:\\Java\\wenku\\src";

	private static String packageName = "sys";

	public static void main(String[] args) {

		String[] entityNames = { "User", "Role", "Privilege" };

		for (String name : entityNames) {
			faction(name);// 实体类名
		}

	}

	private static void faction(String className) {
		try {

			// Action
			{
				String path = getPackagePath(getActionPackageName())
						+ className + "Action.java";
				File file = new File(path);

				if (!file.exists()) {
					FileWriter fw = new FileWriter(path);
					BufferedWriter bw = new BufferedWriter(fw);

					bw.write("package " + getActionPackageName() + ";");
					bw.newLine();
					bw.newLine();
					bw.write("import java.util.List;");
					bw.newLine();
					bw.newLine();
					bw.write("import javax.annotation.Resource;");
					bw.newLine();
					bw.newLine();
					bw
							.write("import org.apache.struts2.convention.annotation.Namespace;");
					bw.newLine();
					bw
							.write("import org.apache.struts2.convention.annotation.Result;");
					bw.newLine();
					bw
							.write("import org.apache.struts2.convention.annotation.Results;");
					bw.newLine();
					bw.write("import org.springside.modules.orm.Page;");
					bw.newLine();
					bw
							.write("import org.springside.modules.orm.PropertyFilter;");
					bw.newLine();
					bw
							.write("import org.springside.modules.utils.web.struts2.Struts2Utils;");
					bw.newLine();
					bw.newLine();
					bw.write("import " + getActionName() + ".BaseAction;");
					bw.newLine();
					bw.write("import " + getBizName() + ".BaseBiz;");
					bw.newLine();
					bw.write("import " + PREFIX
							+ ".exception.ServiceException;");
					bw.newLine();
					bw.write("import " + getEntityPackageName() + "."
							+ className + ";");
					bw.newLine();
					bw.newLine();
					bw.write("@Namespace(\"/" + packageName + "\")");
					bw.newLine();
					bw
							.write("@Results( { @Result(name = BaseAction.RELOAD, location = \""
									+ toLower(className)
									+ ".action\", type = \"redirect\") })");
					bw.newLine();
					bw.write("public class " + className
							+ "Action extends BaseAction<" + className
							+ ", Long> {");
					bw.newLine();
					bw.newLine();
					bw.write("	/**");
					bw.newLine();
					bw.write("	 * 序列化");
					bw.newLine();
					bw.write("	 */");
					bw.newLine();
					bw
							.write("	private static final long serialVersionUID = 1L;");
					bw.newLine();
					bw.newLine();
					bw.write("	@Resource(name = \"" + toLower(className)
							+ "Biz\")");
					bw.newLine();
					bw.write("	public void setBiz(BaseBiz<" + className
							+ ", Long> biz) {");
					bw.newLine();
					bw.write("		super.biz = biz;");
					bw.newLine();
					bw.write("	}");
					bw.newLine();
					bw.newLine();
					bw.write("	/**");
					bw.newLine();
					bw.write("	 * list");
					bw.newLine();
					bw.write("	 */");
					bw.newLine();
					bw.write("	@Override");
					bw.newLine();
					bw.write("	public String list() throws Exception {");
					bw.newLine();
					bw.newLine();
					bw.write("		List<PropertyFilter> filters = PropertyFilter");
					bw.newLine();
					bw
							.write("				.buildFromHttpRequest(Struts2Utils.getRequest());");
					bw.newLine();
					bw.write("		if (!page.isOrderBySetted()) {");
					bw.newLine();
					bw.write("			page.setOrderBy(\"id\");");
					bw.newLine();
					bw.write("			page.setOrder(Page.ASC);");
					bw.newLine();
					bw.write("		}");
					bw.newLine();
					bw.write("		page = biz.search(page, filters);");
					bw.newLine();
					bw.newLine();
					bw.write("		return SUCCESS;");
					bw.newLine();
					bw.write("	}");
					bw.newLine();
					bw.newLine();
					bw.write("	/**");
					bw.newLine();
					bw.write("	 * input");
					bw.newLine();
					bw.write("	 */");
					bw.newLine();
					bw.write("	@Override");
					bw.newLine();
					bw.write("	public String input() throws Exception {");
					bw.newLine();
					bw.newLine();
					bw.write("		return INPUT;");
					bw.newLine();
					bw.write("	}");
					bw.newLine();
					bw.newLine();
					bw.write("	/**");
					bw.newLine();
					bw.write("	 * save");
					bw.newLine();
					bw.write("	 */");
					bw.newLine();
					bw.write("	@Override");
					bw.newLine();
					bw.write("	public String save() throws Exception {");
					bw.newLine();
					bw.newLine();
					bw.write("		biz.save(entity);");
					bw.newLine();
					bw.write("		addActionMessage(\"保存成功\");");
					bw.newLine();
					bw.newLine();
					bw.write("		return RELOAD;");
					bw.newLine();
					bw.write("	}");
					bw.newLine();
					bw.newLine();
					bw.write("	/**");
					bw.newLine();
					bw.write("	 * delete");
					bw.newLine();
					bw.write("	 */");
					bw.newLine();
					bw.write("	@Override");
					bw.newLine();
					bw.write("	public String delete() throws Exception {");
					bw.newLine();
					bw.newLine();
					bw.write("		try {");
					bw.newLine();
					bw.write("			biz.delete(id);");
					bw.newLine();
					bw.write("			addActionMessage(\"删除成功\");");
					bw.newLine();
					bw.write("		} catch (ServiceException e) {");
					bw.newLine();
					bw.write("			logger.error(e.getMessage(), e);");
					bw.newLine();
					bw.write("			addActionMessage(\"删除失败\");");
					bw.newLine();
					bw.write("		}");
					bw.newLine();
					bw.newLine();
					bw.write("		return RELOAD;");
					bw.newLine();
					bw.write("	}");
					bw.newLine();
					bw.newLine();
					bw.write("}");
					bw.close();
				}
			}

			// Biz
			{
				String path = getPackagePath(getBizPackageName()) + className
						+ "Biz.java";
				File file = new File(path);
				if (!file.exists()) {
					FileWriter fw = new FileWriter(path);
					BufferedWriter bw = new BufferedWriter(fw);

					bw.write("package " + getBizPackageName() + ";");
					bw.newLine();
					bw.newLine();
					bw.write("import javax.annotation.Resource;");
					bw.newLine();
					bw.newLine();
					bw
							.write("import org.springframework.stereotype.Repository;");
					bw.newLine();
					bw
							.write("import org.springside.modules.orm.hibernate.HibernateDao;");
					bw.newLine();
					bw.newLine();
					bw.write("import " + getBizName() + ".BaseBiz;");
					bw.newLine();
					bw.write("import " + getEntityPackageName() + "."
							+ className + ";");
					bw.newLine();
					bw.newLine();
					bw.write("@Repository");
					bw.newLine();
					bw.write("public class " + className
							+ "Biz extends BaseBiz<" + className + ", Long> {");
					bw.newLine();
					bw.newLine();
					bw.write("	@Resource(name = \"" + toLower(className)
							+ "Dao\")");
					bw.newLine();
					bw.write("	public void setDao(HibernateDao<" + className
							+ ", Long> dao) {");
					bw.newLine();
					bw.write("		super.dao = dao;");
					bw.newLine();
					bw.write("	}");
					bw.newLine();
					bw.newLine();
					bw.write("}");
					bw.close();
				}
			}

			// Dao
			{
				String path = getPackagePath(getDaoPackageName()) + className
						+ "Dao.java";
				File file = new File(path);
				if (!file.exists()) {
					FileWriter fw = new FileWriter(path);
					BufferedWriter bw = new BufferedWriter(fw);

					bw.write("package " + getDaoPackageName() + ";");
					bw.newLine();
					bw.newLine();
					bw
							.write("import org.springframework.stereotype.Repository;");
					bw.newLine();
					bw.newLine();
					bw.write("import " + getDaoName() + ".BaseDao;");
					bw.newLine();
					bw.write("import " + getEntityPackageName() + "."
							+ className + ";");
					bw.newLine();
					bw.newLine();
					bw.write("@Repository");
					bw.newLine();
					bw.write("public class " + className
							+ "Dao extends BaseDao<" + className + ", Long> {");
					bw.newLine();
					bw.newLine();
					bw.write("}");
					bw.close();
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 转小写（第一个字符转小写）
	 * 
	 * @param str
	 * @return
	 */
	private static String toLower(String str) {
		String sNum = "";
		sNum = str.substring(0, 1).toLowerCase();
		sNum = sNum + str.substring(1);
		return sNum;
	}

	private static String getActionPackageName() {
		return PREFIX + "." + Level.action + "." + packageName;
	}

	private static String getBizPackageName() {
		return PREFIX + "." + Level.biz + "." + packageName;
	}

	private static String getDaoPackageName() {
		return PREFIX + "." + Level.dao + "." + packageName;
	}

	private static String getEntityPackageName() {
		return PREFIX + "." + Level.entity + "." + packageName;
	}

	private static String getActionName() {
		return PREFIX + "." + Level.action;
	}

	private static String getBizName() {
		return PREFIX + "." + Level.biz;
	}

	private static String getDaoName() {
		return PREFIX + "." + Level.dao;
	}

	private static String getPackagePath(String packageName) {
		packageName += ".";
		return srcPath + "\\" + packageName.replaceAll("\\.", "\\\\");
	}

}
